package com.joymeter.data.controller;

import com.alibaba.fastjson.JSONObject;
import com.joymeter.common.base.BaseController;
import com.joymeter.common.utils.HttpClient;
import com.joymeter.common.utils.Result;
import com.joymeter.common.utils.TimeTools;
import com.joymeter.data.entity.DeviceInfo;
import com.joymeter.data.service.DeviceInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 * @ClassName DeviceInfoController
 * @Description TODO
 * @Author liang
 * @Date 2018/8/12 13:16
 * @Version 1.0
 **/
@CrossOrigin("*")
@Controller
@RequestMapping("send")
public class DeviceInfoController extends BaseController {

    @Autowired
    private DeviceInfoService deviceInfoService;

    @RequestMapping(value={"/data"})
    @ResponseBody
    public void sendData( ) throws Exception {
        String url = "http://192.168.25.39:18080/deviceInfo/addEventData";
        String url2 = "http://127.0.0.1:18080/deviceInfo/addEventData";
        while (true){
            DeviceInfo deviceInfo = deviceInfoService.selectByRand();
            String deviceId = deviceInfo.getDeviceId();
            String dataUsed = deviceInfo.getDataUsed()+"1";
            long currentDate = System.currentTimeMillis();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("deviceId",deviceId);
            jsonObject.put("datetime",currentDate);
            jsonObject.put("serverId","000");
            jsonObject.put("type","3100");
            jsonObject.put("event","data");
            jsonObject.put("eventInfo","test");
            jsonObject.put("data",dataUsed);
            Logger.getLogger(DeviceInfoController.class.getName()).log(Level.INFO, "发送data事件"+jsonObject);
            HttpClient.sendPost(url,jsonObject.toString());
        }
      }
    @RequestMapping(value={"/big"})
    @ResponseBody
    public void sendOnline( ) throws Exception {
             try {
                Runnable appScheduler = () -> {
                    try {
                        sendData();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                };
                 for(int i=0;i<10;i++) {
                     Executors.newCachedThreadPool().execute(appScheduler);
                 }
            } catch (Exception e) {
                Logger.getLogger(DeviceInfoController.class.getName()).log(Level.SEVERE, "taskService异常", e);
            }
    }


}
